package com.briup.MR.workflow;

import com.briup.MR.Combiner.SumValue;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
import org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/*
 yarn jar untitled-1.0-SNAPSHOT.jar com.briup.MR.workflow.Avg_work_flow -D input=/user/hdfs/wea.txt -D first_output=/user/hdfs/temp -D output=/user/hdfs/avg_result2
  */
public class Avg_work_flow
        extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf=getConf();

        String input=conf.get("input");
        String first_output=conf.get("first_output");
        String output=conf.get("output");

        Job job= Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("firstAvg");

        job.setMapperClass(FirstAvgTempMR.FirstAvgTempMRMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(SumValue.class);

        job.setReducerClass(FirstAvgTempMR.FirstAvgTempMRReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(SumValue.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(SequenceFileOutputFormat.class);
        TextInputFormat.addInputPath(job,new Path(input));
        SequenceFileOutputFormat.setOutputPath(job,new Path(first_output));

        //第二个job作业的构建
        Job job1=Job.getInstance(conf);
        job1.setJarByClass(this.getClass());
        job1.setJobName("seconfAvg");

        job1.setMapperClass(SecondAvgTempMR.SecondAvgTempMRMapper.class);
        job1.setMapOutputKeyClass(Text.class);
        job1.setMapOutputValueClass(DoubleWritable.class);

        job1.setNumReduceTasks(0);

        job1.setNumReduceTasks(0);

        job1.setInputFormatClass(
                SequenceFileInputFormat.class);
        job1.setOutputFormatClass(
                TextOutputFormat.class);
        SequenceFileInputFormat.addInputPath(job1,
                new Path(first_output));
        TextOutputFormat.setOutputPath(job1,
                new Path(output));

        ControlledJob cron_job=new ControlledJob(conf);
        cron_job.setJob(job);
        ControlledJob cron_job1=new ControlledJob(conf);
        cron_job.setJob(job1);
        //添加依赖关系
        cron_job1.addDependingJob(cron_job);

        //构建作业组
        JobControl jobs=new JobControl("all_avg");
        jobs.addJob(cron_job);
        jobs.addJob(cron_job1);
        //提交作业
        Thread t=new Thread(jobs);
        t.start();

        while(true){
            for (ControlledJob c:jobs.getRunningJobList()){
                c.getJob().monitorAndPrintJob();
            }
            if (jobs.allFinished())break;
        }
        return 0;
    }
    public static void main(String[] args) throws Exception {
        System.exit(
                new ToolRunner().run(
                        new Avg_work_flow(),args));
    }
}
